﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Admin.CYT369.Com.WebBase
{
    public class UserSession
    {
        private Microsoft.AspNetCore.Http.HttpContext m_HttpContext;
        private Admin.CYT369.Com.DAL.Domains.SysUser m_User;


        public UserSession(Microsoft.AspNetCore.Http.IHttpContextAccessor httpContextAccessor)
        {
            if (httpContextAccessor != null && httpContextAccessor.HttpContext != null)
            {
                this.m_HttpContext = httpContextAccessor.HttpContext;
            }
        }

        public Admin.CYT369.Com.DAL.Domains.SysUser GetSessionUser()
        {
            if (m_User == null && m_HttpContext.Session != null)
            {
                var buffer = new byte[0];
                if (m_HttpContext.Session.TryGetValue(Admin.CYT369.Com.Utility.ConstStringClass.SessionUser, out buffer))
                {
                    try
                    {
                        var str = System.Text.Encoding.UTF8.GetString(buffer);
                        m_User = System.Text.Json.JsonSerializer.Deserialize<Admin.CYT369.Com.DAL.Domains.SysUser>(str);
                    }
                    catch { }
                }
            }

            return m_User;
        }

        public string FullName {
            get {
                var user = this.GetSessionUser();
                if (user != null)
                {
                    return user.FullName;
                }

                return null;
            }
        }

        public int GetUserId()
        {
            var user = this.GetSessionUser();
            if (user != null)
            {
                return user.Id;
            }

            return 0;
        }

        public void Clear()
        {
            if (m_HttpContext.Session != null)
            {
                m_HttpContext.Session.Clear();
            }
        }

        /// <summary>
        /// 登录验证码
        /// </summary>
        public string CaptchaCode {
            get {
                if (m_HttpContext.Session != null)
                {
                    var buffer = new byte[0];
                    if (m_HttpContext.Session.TryGetValue(Admin.CYT369.Com.Utility.ConstStringClass.CaptchaCode, out buffer))
                    {
                        return System.Text.Encoding.UTF8.GetString(buffer);
                    }
                }

                return string.Empty;
            }
        }


        public static bool IsAdministrators(Microsoft.AspNetCore.Http.HttpContext httpContext)
        {
            var session = httpContext.RequestServices.GetService(typeof(UserSession)) as UserSession;
            if (session != null)
            {
                var userId = session.GetUserId();
                if (userId > 0)
                {
                    return Admin.CYT369.Com.DAL.Services.UserService.Instance.IsAdministrators(null, userId);
                }
            }

            return false;
        }
    }

}
